package com.tianji.config;

import com.tianji.agent.Agent;
import com.tianji.enums.AgentTypeEnum;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class AgentHolder implements ApplicationContextAware {

    // 按 agent 类型 保存 agent 实例
    private static Map<AgentTypeEnum, Agent> agentMap = new HashMap<>();


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext.getBeansOfType(Agent.class).forEach((k,agent)->{
            agentMap.put(agent.getAgentType(),agent);
        });
    }

    /**
     * 根据 类型 获取 agent
     * @param agentType
     * @return
     */
    public static Agent getAgent(AgentTypeEnum agentType){
        return agentMap.get(agentType);
    }

}